package com.liuqi.aiagent.controller;

import com.liuqi.aiagent.service.VariableService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.dto.VariableEditDto;
import com.liuqi.aiagent.transfer.dto.VariableSaveDto;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.VariableVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.liuqi.aiagent.util.StringUtil.hasText;
import static com.liuqi.aiagent.util.StringUtil.notHasText;
import static com.liuqi.openai.util.StringUtil.isEmpty;

/**
 * @author liuqi
 * @date 2025/7/15
 **/
@RestController
@RequestMapping("/variable")
@AllArgsConstructor
@Api(tags = "系统变量服务接口")
public class VariableController {

    private final VariableService variableService;

    @ApiOperation("添加变量")
    @PostMapping("/add")
    public Result save(@RequestBody VariableSaveDto variableSaveDto) {
        if (notHasText(variableSaveDto.getName())) {
            return ApiResult.fail("变量名称为空");
        }
        if (isEmpty(variableSaveDto.getValue())) {
            return ApiResult.fail("变量值为空");
        }

        return variableService.save(variableSaveDto);
    }

    @ApiOperation("编辑变量")
    @PostMapping("/edit")
    public Result edit(@RequestBody VariableEditDto variableEditDto) {
        if (variableEditDto.getId() == null) {
            return ApiResult.fail("ID为空");
        }
        if (notHasText(variableEditDto.getValue())) {
            return ApiResult.fail("变量值为空");
        }

        return variableService.edit(variableEditDto);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除变量")
    @ApiImplicitParam(name = "id", value = "变量ID", required = true, dataTypeClass = Integer.class)
    public Result delete(Integer id) {
        if (id == null) {
            return ApiResult.fail("ID为空");
        }

        return variableService.delete(id);
    }

    @ApiOperation("查询所有变量")
    @GetMapping("/findAll")
    public ResultList<VariableVo> findAll() {
        return variableService.findAll();
    }

}
